//
//  OMAttributeTextView.m
//  EEEEEEEEEE
//
//  Created by xntech on 13-8-15.
//  Copyright (c) 2013年 xntech. All rights reserved.
//

#import "OMAttributeTextView.h"

@interface OMAttributeTextView()
@property(nonatomic,assign)NSRange preSelectedRange;
@property(nonatomic,assign)CGPoint preContentOffset;
@end

@implementation OMAttributeTextView
@synthesize preSelectedRange;
@synthesize preContentOffset;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setAllowsEditingTextAttributes:YES];
         self.placeholder = @"点击编辑文本";
    }
    return self;
}
-(NSRange)fullRange
{
    return NSMakeRange(0, [[self attributedText] length]);
}
- (void)setFontHoleText:(UIFont *)font
{
    [self setFont:font range:[self fullRange]];
}
- (void)setPreSelectedRange:(NSRange)range
{
    preSelectedRange = range;
    self.preContentOffset = self.contentOffset;
}
- (void)setFont:(UIFont *)font range:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, [[self attributedText] length])];
    [self setAttributedText:attributedString];
    [attributedString release];
}

- (void)setStroke:(UIColor *)color range:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];

    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:1] range:range];
    [attributedString addAttribute:NSStrokeColorAttributeName value:color  range:range];
    [self setAttributedText:attributedString];
    [attributedString release];
}
- (void)setStroke:(UIColor *)color
{
    [self setStroke:color range:[self fullRange]];
}
- (void)removeStroke:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString removeAttribute:NSStrikethroughStyleAttributeName range:range];
    [self setAttributedText:attributedString];
    [attributedString release];
}

- (void)setHighlightColor:(UIColor *)color range:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSBackgroundColorAttributeName value:color  range:range];
    [self setAttributedText:attributedString];
    [attributedString release];

}

- (void)setHighlightColor:(UIColor *)color
{
    [self setHighlightColor:color range:[self fullRange]];
}

- (void)removeHighlightColor:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString removeAttribute:NSBackgroundColorAttributeName range:range];
    [self setAttributedText:attributedString];
    [attributedString release];

}

- (void)setShadow:(NSShadow *)shadow range:(NSRange)range;
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSShadowAttributeName value:shadow  range:range];
    [self setAttributedText:attributedString];
    [attributedString release];

}
- (void)setShadow:(NSShadow *)shadow
{
    [self setShadow:shadow range:[self fullRange]];
}
- (void)removeShawdow:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString removeAttribute:NSShadowAttributeName range:range];
    [self setAttributedText:attributedString];
    [attributedString release];

}

- (void)setColor:(UIColor *)textColor range:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSForegroundColorAttributeName value:textColor  range:range];
    [self setAttributedText:attributedString];
    [attributedString release];

}
- (void)setColor:(UIColor *)textColor
{
    [self setColor:textColor range:[self fullRange]];
}
- (void)setUnderLine:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]  range:range];
    [self setAttributedText:attributedString];
    [attributedString release];
}
- (void)removeUnderLine:(NSRange)range
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString removeAttribute:NSUnderlineStyleAttributeName range:range];
    [self setAttributedText:attributedString];
    [attributedString release];
    
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    [self setSelectedRange:self.preSelectedRange];
    [self setContentOffset:self.preContentOffset animated:NO];
}
- (NSAttributedString *)selectionAtributedString
{
    NSRange range = [self selectedRange];
    NSAttributedString *attribute = [[self attributedText] attributedSubstringFromRange:range];
    return attribute;
}
- (BOOL)isAllValidForKey:(NSString *)key
{
    BOOL hasNext = YES;
    NSAttributedString  *selectString = [self selectionAtributedString];
    for (int i = 0; hasNext; i++) {
        NSRange range = NSMakeRange(0, [selectString length]);
        @try {
            id attribute = [selectString attributesAtIndex:i effectiveRange:&range];
            if (![[attribute allKeys] containsObject:key])
            {
                return NO;
            }
        }
        @catch (NSException *exception) {
            hasNext = NO;
        }
        
    }
    
    return YES;
}
-(BOOL)isUnderLineForSelection
{
    return [self isAllValidForKey:NSUnderlineStyleAttributeName];
}
- (BOOL)isHighLightForSelection
{
    return [self isAllValidForKey:NSBackgroundColorAttributeName];
}
- (BOOL)isStrokeForSelection
{
    return [self isAllValidForKey:NSStrikethroughStyleAttributeName];
}
- (BOOL)isShadowForSelection
{
    return [self isAllValidForKey:NSShadowAttributeName];
}
- (void)setParagraphStyle:(NSMutableParagraphStyle *)style
{
    self.preSelectedRange = self.selectedRange;
    NSMutableAttributedString  *attributedString = [[self attributedText] mutableCopy];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:style  range:[self fullRange]];
    [self setAttributedText:attributedString];
    [attributedString release];
}
- (void)setBoldForSelection
{
    [self toggleBoldface:nil];
}
- (void)setItalicForSelection
{
    [self toggleItalics:nil];
}
@end
